package com.cilico.single.components;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.widget.TextView;

import com.cilico.single.ConfigManager;
import com.cilico.single.R;
import com.cilico.single.components.WaitDialog.Task;
import com.cilico.single.utils.Utils;
import com.cilico.single.view.BaseView;

/**
 * 进度加载�?
 * 
 * @author lv.xiangan
 *
 */
public class ProgressDialog extends Dialog {

	public static Progress PROGRESS;
	static String CONTEXT;

	public ProgressDialog(BaseView baseView, int dialog, Task task, String content) {
		super(baseView, dialog);
		PROGRESS = new Progress(baseView);
		CONTEXT = content;
		setContentView(PROGRESS);
	}

	public static class Progress extends TextView {
		public static Bitmap PROGRESSP; // 加载框转�?
		static Bitmap PROGRESSB; // 加载框背�?
		public static Matrix MAT;
		static Timer timer;
		private int degree_;
		final int textSize_ = 10;

		public Progress(Context context) {
			super(context);
			if (null == PROGRESSP) {
				PROGRESSP = Utils.getScaledBitmap(this.getResources(), R.drawable.progress);
			}
			if (null == PROGRESSB) {
				PROGRESSB = Utils.getScaledBitmap(this.getResources(), R.drawable.progressb);
			}
			if (null == MAT) {
				MAT = new Matrix();
			}
			if (null == timer) {
				timer = new Timer();
			}
		}

		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			setMeasuredDimension(PROGRESSB.getWidth(), PROGRESSB.getHeight());
		}

		public void onDraw(Canvas canvas) {
			String waitMessage = ConfigManager.currentView_.getString(R.string.waitmessage);
			String valuePercent = "";
			// 设置转圈的角度和中心�?
			Paint paint = new Paint();
			paint.setColor(0xFFFFFFFF);
			paint.setTextSize(Utils.getScaledValue(textSize_));
			final int x = (PROGRESSB.getWidth() - PROGRESSP.getWidth()) >> 1;
			final int yBmp = (PROGRESSB.getWidth() >> 1) - PROGRESSP.getWidth();
			MAT.setRotate(degree_, PROGRESSP.getWidth() >> 1, PROGRESSP.getHeight() >> 1);
			MAT.postTranslate(x, yBmp);
			degree_ = degree_ + 45;
			if (degree_ == 360)
				degree_ = 0;
			// 绘制背景图片
			canvas.drawBitmap(PROGRESSB, 0, 0, paint);
			// 绘制进度旋转图片
			canvas.drawBitmap(PROGRESSP, MAT, paint);
			if (null != CONTEXT && CONTEXT.contains("%") && (waitMessage.length() < CONTEXT.length())) {
				try {
					valuePercent = CONTEXT.substring(waitMessage.length());
					CONTEXT = CONTEXT.substring(0, waitMessage.length());
				} catch (Exception e) {
					Utils.printException(e);
				}
			}
			if (null != CONTEXT)
				canvas.drawText(CONTEXT, (PROGRESSB.getWidth() - (int) paint.measureText(CONTEXT)) >> 1,
						Utils.getUserDeviceScaledValue((PROGRESSB.getHeight() >> 1) + textSize_ * 2), paint);
			if (!valuePercent.equals("")) {
				canvas.drawText(valuePercent, (PROGRESSB.getWidth() - (int) paint.measureText(valuePercent)) >> 1,
						Utils.getUserDeviceScaledValue((PROGRESSB.getHeight() >> 1) + (textSize_ << 1)  * 2), paint);
			}
			timer.schedule(new TimerTask() {

				@Override
				public void run() {
					ProgressDialog.Progress.this.postInvalidate();
				}

			}, 100);
		}
	}

	public void setMessage(String loadingStatus) {
		CONTEXT = loadingStatus;
	}

	/** 
	 * 按键操作
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			BaseView.mid_.waitDialog_.cancelAndRemoveTask(null);
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
